package com.example.statistics.common

import android.os.Bundle
import android.view.View
import com.example.statistics.ex.loadingDialog
import kotlinx.android.synthetic.main.layout_search_bar.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

abstract class BaseCommonListFragment<T> : BaseListFragment<T>() {

    //输入框变化就开始搜索
    open val inputSearchFunc: (T, String) -> Boolean = { _, _ -> true }
    abstract val localDataList: List<T>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        adapterLocal.setOnLoadMoreListener { it.loadMoreEnd() }
        refresh()
    }

    override fun refresh() {
        GlobalScope.launch(Dispatchers.Main) {
            dataList = localDataList
            val dialog = context?.loadingDialog { }
            val list = dataList.filter { inputSearchFunc(it, mParentView.et_search_input.text.toString()) }
            dataListEmpty(list)
            dialog?.dismiss()
        }
    }

    override fun inputSearch(search: String?) {
        refresh()
    }
}